package com.zsg.androidsipphone.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.zsg.androidsipphone.R;
import com.zsg.androidsipphone.bean.Login;
import com.zsg.androidsipphone.ui.fragment.impl.EndFragmentService;

import java.util.Objects;

/**
 * @author zsg
 */
public class EndFragment extends EndFragmentService {

    @SuppressLint("StaticFieldLeak")
    public static TextView userName;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_end, container, false);
    }

    @Override
    public void onViewCreated(@NonNull  View view, @Nullable  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取控件并绑定监听事件
        NavigationView navigationView = requireView().findViewById(R.id.nav_person_center);
        View header = navigationView.getHeaderView(0);
        //显示登录的用户名
        userName = header.findViewById(R.id.text_user_name);
        //配置监听
        navigationView.setNavigationItemSelectedListener(menuItem -> {
            View view1 = View.inflate(requireContext(),R.layout.dialog_login,null);
            TextInputEditText input1 = view1.findViewById(R.id.input_login_phone);
            TextInputEditText input2 = view1.findViewById(R.id.input_login_password);

            switch (menuItem.getItemId()){
                case R.id.nav_login:
                    if ("我的".equals(userName.getText().toString())){
                        //未登录弹出登录界面
                        new AlertDialog.Builder(requireContext())
                                .setTitle("登录")
                                .setView(view1)
                                .setPositiveButton("登录", (dialogInterface, i) -> {
                                    String phone = Objects.requireNonNull(input1.getText()).toString();
                                    String pass = Objects.requireNonNull(input2.getText()).toString();
                                    if (!phone.isEmpty() && !pass.isEmpty()){
                                        Login login = new Login(phone,pass);
                                        //调用登录
                                        super.login(login);
                                        userName.setText("登录中...");
                                    }else {
                                        Toast.makeText(requireContext(),"请完善登录信息！",Toast.LENGTH_SHORT).show();
                                    }

                                })
                                .setNegativeButton("取消",null)
                                .show();
                    }else {
                        //已登录打开我的信息
                        Snackbar.make(requireView(),"我的信息",Snackbar.LENGTH_SHORT).show();
                    }
                    return true;
                case R.id.nav_settings:
                    Snackbar.make(requireView(),"settings",Snackbar.LENGTH_SHORT).show();
                    return true;
                case R.id.nav_logout:
                    userName.setText("我的");
                    return true;
                default:
                    break;
            }
            return false;
        });

    }
}